package org.mclll.xwangplugin.mclllFix.listener;

import lombok.AllArgsConstructor;
import lombok.Data;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.mclll.xwangplugin.MclllCustom;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class UseBedListener implements Listener {

    private MclllCustom plugin;

    private static final Map<UUID, SetHomeNode> SET_HOME_NODE_HASH_MAP = new HashMap<>();
    private static final int COOL_DOWN = 5000;

    public UseBedListener(MclllCustom plugin) {
        this.plugin = plugin;
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void UseBed(PlayerInteractEvent event) {
        if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
            Block clickedBlock = event.getClickedBlock();
            if (clickedBlock != null && clickedBlock.getType().name().contains("_BED")) {
                Location home = clickedBlock.getLocation();
                String setHomeCommand = "sethome Home %s -l:%s;%d;%d;%d";
                Player player = event.getPlayer();
                player.sendMessage(
                        Component.text("输入“是”以重新设置重生点").color(NamedTextColor.YELLOW)
                );
                SET_HOME_NODE_HASH_MAP.put(player.getUniqueId(), new SetHomeNode(System.currentTimeMillis(),
                        String.format(setHomeCommand, player.getName(), home.getWorld().getName(), home.getBlockX(), home.getBlockY(), home.getBlockZ())));
            }
        }
    }

    @Data
    @AllArgsConstructor
    private static class SetHomeNode {
        private long time;
        private String command;
    }

    @EventHandler
    public void confirmSetHome(final AsyncPlayerChatEvent event) {
        Player player = event.getPlayer();
        String message = event.getMessage();
        if ("是".equals(message)) {
            SetHomeNode setHomeNode = SET_HOME_NODE_HASH_MAP.getOrDefault(player.getUniqueId(), null);
            if (setHomeNode == null) {
                return;
            }
            event.setMessage("");
            event.setCancelled(true);
            if (System.currentTimeMillis() - setHomeNode.getTime() <= COOL_DOWN) {
                Bukkit.getServer().getScheduler().runTask(plugin, () -> {
                    Bukkit.getServer().dispatchCommand(
                            Bukkit.getConsoleSender(), setHomeNode.getCommand()
                    );
                });
                player.sendMessage(
                        Component.text("设置家成功!").color(NamedTextColor.GREEN)
                );

            } else {
                player.sendMessage(
                        Component.text("设置家超时!!").color(NamedTextColor.RED)
                );
            }
            SET_HOME_NODE_HASH_MAP.remove(player.getUniqueId());
        }
    }
}
